package rattlesnake.callback.rest;

import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import rattlesnake.callback.config.constant.CallBackConstant;
import rattlesnake.callback.core.cache.CacheManager;
import rattlesnake.callback.modle.vo.ResponseVo;
import rattlesnake.callback.service.AsyncService;
import rattlesnake.callback.util.MJsonBuilder;
import rattlesnake.callback.util.ObjectUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @Package: rattlesnake.callback.rest
 * @Description: 渠道控制
 * URL命名规则[固定必须]=服务名+渠道名+order_status
 * @author: liuxin
 * @date: 17/4/10 下午3:11
 */
@RestController
public class ChannelController {
    private static final Logger logger = LoggerFactory.getLogger(ChannelController.class);
//    @Autowired
//    AsyncService asyncService;

    /**
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/shenghuojiaofei/chinapay/order_status", method = RequestMethod.GET)
    public String test(HttpServletRequest request, HttpServletResponse response) {
        String messageCode = (String) request.getAttribute(CallBackConstant.MESSAGECODE);
        logger.info("------------------");
        return new MJsonBuilder().set("code", 0, Integer.class).set("date", messageCode, String.class).set("msg", "处理成功", String.class).toJson();
    }

    //
    @RequestMapping(value = "/chinapay/order_status", method = RequestMethod.POST)
    public ResponseVo Life(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String messageCode = (String) request.getAttribute(CallBackConstant.MESSAGECODE);
        Enumeration<String> parameter = request.getParameterNames();
        Map<String, String> map = new HashMap<>();
        while (parameter.hasMoreElements()) {
            String key = parameter.nextElement();
            String value = request.getParameter(key);
            logger.info(key + "--" + value);
            map.put(key, value);
        }
        System.out.println(new Gson().toJson(
                map
        ));
        return new ResponseVo(map);
    }


}
